﻿using System;
using System.Collections.Generic;
using System.Text;
using iMES.Core.Utilities;
using iMES.Entity.DomainModels;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using YUN.Rawmanagements.IRepositories;

namespace YUN.Rawmanagements.Services
{
    public partial class Base_RawmanagementServies
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBase_RawmanagementRepository _repository;//访问数据库
        private readonly IBase_RawmanagementRepository _numberRuleRepository;//自定义编码规则访问数据库


        [ActivatorUtilitiesConstructor]
        public Base_RawmanagementServies(
         IBase_RawmanagementRepository dbRepository,
         IBase_RawmanagementRepository numberRuleRepository,
         IHttpContextAccessor httpContextAccessor
         )
     : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _numberRuleRepository = numberRuleRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            AddOnExecuting = (Rawmanagement tool, object list) =>
            {
                //if (string.IsNullOrWhiteSpace(tool.ToolCode))
                //    tool.ToolCode = GetToolCode();
                ////如果返回false,后面代码不会再执行
                //if (repository.Exists(x => x.ToolCode == tool.ToolCode))
                //{
                //    return webResponse.Error("工装编码已存在");
                //}
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveDataModel)
        {
            //编辑方法保存数据库前处理
            UpdateOnExecuting = (Rawmanagement defectItem, object addList, object updateList, List<object> delKeys) =>
            {
                //if (repository.Exists(x => x.DefectItemName == defectItem.DefectItemName && x.DefectItem_Id != defectItem.DefectItem_Id))
                //{
                //    return webResponse.Error("不良品项名称已存在");
                //}
                //if (repository.Exists(x => x.DefectItemCode == defectItem.DefectItemCode && x.DefectItem_Id != defectItem.DefectItem_Id))
                //{
                //    return webResponse.Error("不良品项编号已存在");
                //}
                return webResponse.OK();
            };
            base.UpdateOnExecuted = (Rawmanagement defectItem, object obj1, object obj2, List<object> List) =>
            {
                return new WebResponseContent(true);
            };
            return base.Update(saveDataModel);
        }


    }
}
